import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { User } from '../../auth/entities/user.entity';

/**
 * 作品类别枚举
 */
export enum WorkCategoryEnum {
  FOOD = 'food', // 美食
  HEALTH = 'health', // 养生
  TRAVEL = 'travel', // 旅游
}

/**
 * 作品状态枚举
 */
export enum WorkStatusEnum {
  DRAFT = 'draft', // 草稿
  PUBLISHED = 'published', // 已发布
  ARCHIVED = 'archived', // 已归档
}

/**
 * 媒体类型枚举
 */
export enum MediaTypeEnum {
  IMAGE = 'image', // 图片
  VIDEO = 'video', // 视频
}

/**
 * 作品实体
 */
@Entity('works')
export class Work {
  @PrimaryGeneratedColumn()
  id: number;

  // 作品基本信息
  @Column({ length: 200, comment: '作品标题' })
  title: string;

  @Column({ type: 'text', comment: '作品描述' })
  description: string;

  @Column({
    type: 'enum',
    enum: WorkCategoryEnum,
    comment: '作品类别',
  })
  category: WorkCategoryEnum;

  @Column({
    type: 'enum',
    enum: WorkStatusEnum,
    default: WorkStatusEnum.DRAFT,
    comment: '作品状态',
  })
  status: WorkStatusEnum;

  @Column({ default: true, comment: '是否公开' })
  isPublic: boolean;

  // 媒体信息
  @Column({
    type: 'enum',
    enum: MediaTypeEnum,
    comment: '媒体类型',
  })
  mediaType: MediaTypeEnum;

  @Column({
    type: 'simple-json',
    nullable: true,
    comment: '图片URL列表（JSON数组）',
  })
  imagesUrls: string[];

  @Column({ length: 500, nullable: true, comment: '视频URL' })
  videoUrl: string;

  @Column({ length: 500, nullable: true, comment: '视频封面URL' })
  videoCover: string;

  // 标签
  @Column({
    type: 'simple-json',
    nullable: true,
    comment: '作品标签（JSON数组）',
  })
  tags: string[];

  // 统计信息
  @Column({ default: 0, comment: '浏览量' })
  viewsCount: number;

  @Column({ default: 0, comment: '点赞数' })
  likesCount: number;

  @Column({ default: 0, comment: '收藏数' })
  collectionsCount: number;

  @Column({ default: 0, comment: '评论数' })
  commentsCount: number;

  @Column({ default: 0, comment: '分享数' })
  sharesCount: number;

  // 关联用户
  @Column({ comment: '作者ID' })
  authorId: number;

  @ManyToOne(() => User, { eager: false })
  @JoinColumn({ name: 'authorId' })
  author: User;

  // 位置信息（旅游作品可能需要）
  @Column({ length: 200, nullable: true, comment: '地点名称' })
  location: string;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 7,
    nullable: true,
    comment: '纬度',
  })
  latitude: number;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 7,
    nullable: true,
    comment: '经度',
  })
  longitude: number;

  // 发布时间（与创建时间不同，草稿转发布时更新）
  @Column({ type: 'timestamp', nullable: true, comment: '发布时间' })
  publishedAt: Date | null;

  @CreateDateColumn({ type: 'timestamp', comment: '创建时间' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', comment: '更新时间' })
  updatedAt: Date;
}
